﻿using Arch.EntityFrameworkCore.UnitOfWork;
using AutoMapper;
using Common.Lib.Dto;
using Common.Lib.Parameter;
using Common.Lib.Service;
using Server.Todo.Context;

namespace Server.Todo.Service
{
    public class MemoService : IMemoService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper;
        private readonly IRepository<Memo> repository;

        public MemoService(IUnitOfWork unitOfWork,IMapper mapper)
        {
            this.unitOfWork = unitOfWork;
            this.mapper = mapper;
            this.repository = unitOfWork.GetRepository<Memo>();
        }

        public async Task<ApiResponse> AddAsync(MemoDto model)
        {
            try
            {
                var dto = mapper.Map<Memo>(model);
                await unitOfWork.GetRepository<Memo>().InsertAsync(dto);
                if (await unitOfWork.SaveChangesAsync() > 0)
                {
                    return new ApiResponse(true, model);
                }
                return new ApiResponse("添加数据失败");
            }
            catch(Exception ex)
            {
                return new ApiResponse($"添加数据失败:{ex.Message}");
            }
        }

        public async Task<ApiResponse> DeleteAsync(int id)
        {
            try
            {
                var repository = unitOfWork.GetRepository<Memo>();
                var dto = await repository.GetFirstOrDefaultAsync(predicate: p=>p.Id.Equals(id));
                repository.Delete(dto);

                if (await unitOfWork.SaveChangesAsync() > 0)
                {
                    return new ApiResponse(true,null);
                }
                return new ApiResponse("删除数据失败");
            }
            catch (Exception ex)
            {
                return new ApiResponse($"删除数据失败:{ex.Message}");
            }
        }

        public async Task<ApiResponse> GetAllAsync(QueryParameter queryParameter)
        {
            try
            {
                var repository = unitOfWork.GetRepository<Memo>();
                var todos = await repository.GetPagedListAsync(
                    predicate: p => string.IsNullOrEmpty(queryParameter.SearchKey) ? true : p.Title.Contains(queryParameter.SearchKey),
                    pageSize: queryParameter.PageSize,
                    pageIndex: queryParameter.PageIndex,
                    orderBy: o => o.OrderByDescending(p => p.Id));
                return new ApiResponse(true, todos);
            }
            catch (Exception ex)
            {
                return new ApiResponse($"查询所有数据失败:{ex.Message}");
            }
        }

        public async Task<ApiResponse> GetSingleAsync(int id)
        {
            try
            {
                var repository = unitOfWork.GetRepository<Memo>();
                var dto = await repository.GetFirstOrDefaultAsync(predicate: p=>p.Id.Equals(id));
                return new ApiResponse(true, dto);
            }
            catch (Exception ex)
            {
                return new ApiResponse($"查询单条数据失败:{ex.Message}");
            }
        }

        public async Task<ApiResponse> UpdateAsync(MemoDto model)
        {
            try
            {
                var dto = mapper.Map<Memo>(model);
                await unitOfWork.GetRepository<Memo>().InsertAsync(dto);
                var repository = unitOfWork.GetRepository<Memo>();
                repository.Update(dto);
                if (await unitOfWork.SaveChangesAsync() > 0)
                {
                    return new ApiResponse(true, model);
                }
                return new ApiResponse("更新数据失败");
            }
            catch (Exception ex)
            {
                return new ApiResponse($"更新数据失败:{ex.Message}");
            }
        }
    }
}
